/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rcn.hibernate;

import java.io.File;
import java.io.InputStream;
import org.hibernate.FlushMode;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public class HibernateUtil {

    private static final SessionFactory sessionFactory;
    static {
        try {
            String file = HibernateUtil.class.getClassLoader().getResource("hibernate.cfg.xml").getPath().toString();
            // Si no ponemos fichero, intenta cargar "/hibernate.cfg.xml" en el raiz
            sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
            
        } catch (Throwable ex) {
            // Log exception!
            System.out.println("ERROR: "+ex.getMessage());
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static Session getSession() throws HibernateException {
        return sessionFactory.openSession();
    }
}
